package com.vibration.baseinfo.service.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.vibration.baseinfo.domain.CzCalculateTxValue;
import com.vibration.baseinfo.domain.CzGroundWaterData;
import com.vibration.baseinfo.domain.CzMoreTemperatureData;
import com.vibration.baseinfo.mapper.CzCalculateTxValueMapper;
import com.vibration.baseinfo.service.ICzCalculateTxValueService;
import com.vibration.baseinfo.service.ICzMoreTemperatureDataService;
import com.vibration.common.core.domain.AjaxResult;
import com.vibration.common.utils.DateUtils;
import com.vibration.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vibration.baseinfo.mapper.CzCalculateTxMapper;
import com.vibration.baseinfo.domain.CzCalculateTx;
import com.vibration.baseinfo.service.ICzCalculateTxService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * Tx计算记录Service业务层处理
 * 
 * @author Mr_Wu
 * @date 2023-03-07
 */
@Service
public class CzCalculateTxServiceImpl implements ICzCalculateTxService 
{
    @Autowired
    private CzCalculateTxMapper czCalculateTxMapper;

    @Autowired
    private ICzMoreTemperatureDataService czMoreTemperatureDataService;

    @Autowired
    private ICzCalculateTxValueService calculateTxValueService;




    /**
     * 查询Tx计算记录
     * 
     * @param id Tx计算记录主键
     * @return Tx计算记录
     */
    @Override
    public CzCalculateTx selectCzCalculateTxById(Long id)
    {
        CzCalculateTx calculateTx = czCalculateTxMapper.selectCzCalculateTxById(id);
        List<CzCalculateTxValue> txValueList = calculateTxValueService.selectCzCalculateTxValueList(calculateTx.getId());
        calculateTx.setTxValueList(txValueList);
        return calculateTx;
    }

    /**
     * 查询Tx计算记录列表
     * 
     * @param czCalculateTx Tx计算记录
     * @return Tx计算记录
     */
    @Override
    public List<CzCalculateTx> selectCzCalculateTxList(CzCalculateTx czCalculateTx)
    {
        return czCalculateTxMapper.selectCzCalculateTxList(czCalculateTx);
    }

    /**
     * 新增Tx计算记录
     * 
     * @param czCalculateTx Tx计算记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCzCalculateTx(CzCalculateTx czCalculateTx)
    {
        //查询所有温度数据
        CzMoreTemperatureData param = new CzMoreTemperatureData();
        param.setEquipmentCode(czCalculateTx.getEquipmentCode());
        param.setParams(czCalculateTx.getParams());
        List<CzMoreTemperatureData> temperatureDataList=czMoreTemperatureDataService.calculation(param);
        //获取checkList截取数据
        List<String> subComputeList=czCalculateTx.getCheckList();
        String degreeDepth = "";
        for (int i = 0; i < subComputeList.size(); i++) {
            if(i==0){
                degreeDepth+=subComputeList.get(i);
            }else {
                degreeDepth+= "-"+subComputeList.get(i);
            }
        }
        czCalculateTx.setDegreeDepth(degreeDepth);
        czCalculateTx.setStartTime(DateUtils.parseDate(czCalculateTx.getParams().get("beginTime")));
        czCalculateTx.setEndTime(DateUtils.parseDate(czCalculateTx.getParams().get("endTime")));
        czCalculateTx.setCreateTime(DateUtils.getNowDate());
        //参数一集合
        List<Double> dataT1=new ArrayList<>();
        //参数二集合
        List<Double> dataT2=new ArrayList<>();
        String tempComputeT1=subComputeList.get(0);
        String tempComputeT2=subComputeList.get(1);
        for (CzMoreTemperatureData data : temperatureDataList) {
            if("5m".equals(tempComputeT1)){
                dataT1.add(data.getTemperature5());
            }else if("10m".equals(tempComputeT1)){
                dataT1.add(data.getTemperature10());
            }else if("15m".equals(tempComputeT1)){
                dataT1.add(data.getTemperature15());
            }else if("20m".equals(tempComputeT1)){
                dataT1.add(data.getTemperature20());
            }else if("25m".equals(tempComputeT1)){
                dataT1.add(data.getTemperature25());
            }else if("30m".equals(tempComputeT1)){
                dataT1.add(data.getTemperature30());
            }else if("35m".equals(tempComputeT1)){
                dataT1.add(data.getTemperature35());
            }else if("40m".equals(tempComputeT1)){
                dataT1.add(data.getTemperature40());
            }else if("45m".equals(tempComputeT1)){
                dataT1.add(data.getTemperature45());
            }else if("50m".equals(tempComputeT1)){
                dataT1.add(data.getTemperature50());
            }

            if("5m".equals(tempComputeT2)){
                dataT2.add(data.getTemperature5());
            }else if("10m".equals(tempComputeT2)){
                dataT2.add(data.getTemperature10());
            }else if("15m".equals(tempComputeT2)){
                dataT2.add(data.getTemperature15());
            }else if("20m".equals(tempComputeT2)){
                dataT2.add(data.getTemperature20());
            }else if("25m".equals(tempComputeT2)){
                dataT2.add(data.getTemperature25());
            }else if("30m".equals(tempComputeT2)){
                dataT2.add(data.getTemperature30());
            }else if("35m".equals(tempComputeT2)){
                dataT2.add(data.getTemperature35());
            }else if("40m".equals(tempComputeT2)){
                dataT2.add(data.getTemperature40());
            }else if("45m".equals(tempComputeT2)){
                dataT2.add(data.getTemperature45());
            }else if("50m".equals(tempComputeT2)){
                dataT2.add(data.getTemperature50());
            }
        }

        int count = czCalculateTxMapper.insertCzCalculateTx(czCalculateTx);

        tempComputeT1=subComputeList.get(0).substring(0,subComputeList.get(0).lastIndexOf("m"));
        tempComputeT2=subComputeList.get(1).substring(0,subComputeList.get(1).lastIndexOf("m"));
        //k值
        Double K= czCalculateTx.getK();
        //高度
        Double H=czCalculateTx.getH();
        Double m=czCalculateTx.getLm();
        //参数一深度
        Double D1=Double.parseDouble(tempComputeT1);
        //参数二深度
        Double D2=Double.parseDouble(tempComputeT2);
        //传参
        List<Double> TxDataList=new ArrayList<>();
        List<Double> qDataList=new ArrayList<>();

        List<CzCalculateTxValue> txValueList = new ArrayList<>();
        for (int i=0;i<dataT1.size();i++) {
            double getTx=dataT1.get(i)+K*((dataT2.get(i)-dataT1.get(i))/(D2-D1))*H/m;
            double qData=K*((dataT2.get(i)-dataT1.get(i))/(D2-D1));
            qDataList.add(qData);
            TxDataList.add(getTx);
            CzCalculateTxValue txValue =  new CzCalculateTxValue();
            txValue.setCalculateId(czCalculateTx.getId());
            txValue.setT1(dataT1.get(i));
            txValue.setT2(dataT2.get(i));
            txValue.setQ(formatDouble(qData,3));
            txValue.setTx(formatDouble(getTx,2));
            txValue.setDataTime(temperatureDataList.get(i).getDataTime());
            txValueList.add(txValue);
        }
         calculateTxValueService.insertCalculateTxValue(txValueList);
        return count;
    }

    /**
     * 修改Tx计算记录
     * 
     * @param czCalculateTx Tx计算记录
     * @return 结果
     */
    @Override
    public int updateCzCalculateTx(CzCalculateTx czCalculateTx)
    {
        return czCalculateTxMapper.updateCzCalculateTx(czCalculateTx);
    }

    /**
     * 批量删除Tx计算记录
     * 
     * @param ids 需要删除的Tx计算记录主键
     * @return 结果
     */
    @Override
    public int deleteCzCalculateTxByIds(Long[] ids)
    {
        //删除相关记录
        calculateTxValueService.deleteCzCalculateTxValueByTxIds(ids);

        return czCalculateTxMapper.deleteCzCalculateTxByIds(ids);
    }

    /**
     * 删除Tx计算记录信息
     * 
     * @param id Tx计算记录主键
     * @return 结果
     */
    @Override
    public int deleteCzCalculateTxById(Long id)
    {
        return czCalculateTxMapper.deleteCzCalculateTxById(id);
    }

    //double保留小数位
    public static double formatDouble(double d,int n ) {
        // 新方法，如果不需要四舍五入，可以使用RoundingMode.DOWN
        BigDecimal bg = new BigDecimal(d).setScale(n, RoundingMode.HALF_UP);
        return bg.doubleValue();
    }


    /**
     * 导入excel生成曲线图
     * @param file
     * @param czCalculateTx
     * @return
     * @throws Exception
     */
    public CzCalculateTx importExcelData(MultipartFile file, CzCalculateTx czCalculateTx) throws Exception {
        int count=czCalculateTxMapper.insertCzCalculateTx(czCalculateTx);
        Long calculateId=czCalculateTx.getId();
        //获取导入温度数据
        ExcelUtil<CzCalculateTxValue> util = new ExcelUtil<CzCalculateTxValue>(CzCalculateTxValue.class);
        List<CzCalculateTxValue> dataList = util.importExcel(file.getInputStream());
        calculateTxValueService.batchInsertCzCalculateTxValue(calculateId,dataList);
        List<CzCalculateTxValue> TemperatureList=calculateTxValueService.selectCzCalculateTxValueList(calculateId);

        List<Double> T1 = new ArrayList<>();
        List<Double> T2 = new ArrayList<>();
        for (CzCalculateTxValue czCalculateTxValue : TemperatureList) {
            T1.add(czCalculateTxValue.getT1());
            T2.add(czCalculateTxValue.getT2());
        }
        Map<String,List<Double>> chartMap = new HashMap<>();
        chartMap.put("T1",T1);
        chartMap.put("T2",T2);
        JSONObject object = new JSONObject();
        object.put("chartData",chartMap);
        czCalculateTx.setTempDataList(object);
        return czCalculateTx;
    }


    /**
     * 新增Tx的Excel计算
     */
    public CzCalculateTx insertCzCalculateTxExcelCompute(CzCalculateTx czCalculateTx){
        czCalculateTxMapper.updateCzCalculateTx(czCalculateTx);
        List<CzCalculateTxValue> TemperatureList=calculateTxValueService.selectCzCalculateTxValueList(czCalculateTx.getId());
        List<Double> T1=new ArrayList<>();
        List<Double> T2=new ArrayList<>();
        List<Double> KList=new ArrayList<>();
        List<Double> HList=new ArrayList<>();
        List<Double> MList=new ArrayList<>();
        List<Double> DeepOneList=new ArrayList<>();
        List<Double> DeepTwoList=new ArrayList<>();
        for (CzCalculateTxValue ccltv:
             TemperatureList) {
            T1.add(ccltv.getT1());
            T2.add(ccltv.getT2());
            KList.add(ccltv.getK());
            HList.add(ccltv.getH());
            MList.add(ccltv.getLm());
            DeepOneList.add(ccltv.getDeepOne());
            DeepTwoList.add(ccltv.getDeepTwo());
        }
        List<Double> qDataList=new ArrayList<>();
        List<Double> TxDataList=new ArrayList<>();
        List<CzCalculateTxValue> result=new ArrayList<>();

        for (int i=0;i<T1.size();i++){
            double getTx=T1.get(i)+(KList.get(i)*((T2.get(i)-T1.get(i))/(DeepTwoList.get(i)-DeepOneList.get(i)))* HList.get(i))/MList.get(i);
            double qData=KList.get(i)*(((T2.get(i)-T1.get(i))/(DeepTwoList.get(i)-DeepOneList.get(i))));
            qDataList.add(qData);
            TxDataList.add(getTx);
            CzCalculateTxValue txValue =  new CzCalculateTxValue();
            txValue.setId(TemperatureList.get(i).getId());
            txValue.setCalculateId(czCalculateTx.getId());
            txValue.setT1(T1.get(i));
            txValue.setT2(T2.get(i));
            txValue.setQ(formatDouble(qData,3));
            txValue.setTx(formatDouble(getTx,2));
            result.add(txValue);
        }
        calculateTxValueService.batchUpdateCzCalculateTxValue(result);
        return czCalculateTx;
    }
}
